package view 
{
	
	import com.greensock.TweenMax;
	import controller.Const;
	import controller.GameController;
	import controller.RequestController;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.utils.Timer;
	import model.GameData;
	import model.MyData;
	import sound.SoundManager;
	import view.popup.PopupResultBetting;
	import view.popup.Tooltip;
	import view.popup.TooltipTimeAndQTT;
	
	/**
	 * ...
	 * @author bimkute
	 */
	public class Betting extends Base 
	{
		private var _logo:MovieClip;
		//private var _showMoney:MovieClip;
		private var _showTime:MovieClip;
		private var _bettingFish:MovieClip;
		private var _betchawnsxChanLe:MovieClip;
		private var _historyBet:MovieClip;
		private var _historyChangeMoney:MovieClip;
		private var _totalSum:MovieClip;
		private var _settingSound:MovieClip;
		private var _totalBetMoney:MovieClip;
		private var _betArr:Array;
		private var _multiArr:Array;
		
		private var _timeRemain:int;
		private var _timeOutGetResult:int;
		
		private var _timer:Timer;
		private var timerGet:Timer;
		private var timerTest:Timer;
		
		private var _count:int = 0;
		private var i:int;
		
		private var _popupResultBetting:PopupResultBetting;
		private var _popupTutorial:TutorialBoard;
		
		private var _objectTotal:Object;
		private var _objectOne:Object;
		
		private var _tipAll:Tooltip;
		private var _tipQTT:TooltipTimeAndQTT;
		private var _arrImageAnimal:Array = [];
		private var _arrAnimalHS:Array = [];
		private var _countName:int;
		private var _timeShowQTT:Timer = new Timer(8000)
		private var _timeVisibleQTT:Timer = new Timer(2000)
		public function Betting(money:int) 
		{
			_betArr = [];
			_multiArr = [];
			
			_logo = getImage("logoGame", "playGame");
			//_showMoney = getImage("Bg_showVcoin", "playGame");
			_showTime = getImage("Bg_showTime", "playGame");
			_bettingFish = getImage("Board_Betting", "playGame");
			_betchawnsxChanLe = getImage("Board_ChanLe", "playGame");
			_historyBet = getImage("HistoryBet", "playGame");
			_totalBetMoney = getImage("sum_betting", "playGame");
			//_historyChangeMoney = getImage("HistoryChangeMoney", "playGame");
			_totalSum = getImage("bg_bangthantai", "playGame");
			_settingSound = _historyBet.btn_sound;
			
			addChild(_logo);
			//addChild(_showMoneyi);
			addChild(_showTime);
			addChild(_bettingFish);
			addChild(_betchawnsxChanLe);
			addChild(_historyBet);
			addChild(_totalBetMoney);
			//addChild(_historyChangeMoney);
			addChild(_totalSum);
			//addChild(_settingSound);
			
			_logo.x = 100;
			_logo.y = 120;
			
			//_showMoney.x = 80;
			//_showMoney.y = 140;
			
			_showTime.x = 700;
			_showTime.y = 140;
			
			_totalSum.x = 280;
			_totalSum.y = 130;
			
			_bettingFish.x = 970;
			_bettingFish.y = 170;
			
			_betchawnsxChanLe.x = 960;
			_betchawnsxChanLe.y = 620;
			
			_historyBet.x = 230;
			_historyBet.y = 580;
			
			_totalBetMoney.x = 980;
			_totalBetMoney.y = 700;
			
			
			_arrImageAnimal = [	_bettingFish.conca, _bettingFish.concua, _bettingFish.consua, _bettingFish.conoc, _bettingFish.contom,
								_bettingFish.conruabien, _bettingFish.concangua, _bettingFish.conranbien, _bettingFish.consam, _bettingFish.concaugai];
								
			//_historyChangeMoney.x = 1100;
			//_historyChangeMoney.y = 140;
			
			//_settingSound.x = 1300;
			//_settingSound.y = 140;
			
			
			
			_settingSound.buttonMode = true;
			
			if (GameData.getInstance().checkSoundOn) 
			{
				_settingSound.gotoAndStop(1);
			}
			else 
			{
				_settingSound.gotoAndStop(2);
			}
			
			_settingSound.addEventListener(MouseEvent.CLICK, onClickSoundOnOff);
			
			//_showMoney.txtCoin.text = format(money) + " Vcoin";
			_showTime.txtTime.text = "Thời gian: " + String(0);
			//_showMoney.txtCoin.maxChars
			for (i = 0; i < 10; i++) 
			{
				_betArr.push(_bettingFish["txtBet" + String(i + 1)]);
				_betArr[i].text = "Nhập sao";
				_betArr[i].restrict = "0-9";
				_betArr[i].maxChars = 9;
				_multiArr.push(_bettingFish["txtMultiplier" + String(i + 1)]);
				_multiArr[i].text = "x 1";
				
			}
			_betchawnsxChanLe["txtChan"].text = "Nhập sao";
			_betchawnsxChanLe["txtChan"].restrict = "0-9";
			_betchawnsxChanLe["txtChan"].maxChars = 9;
			_betchawnsxChanLe["txtLe"].text = "Nhập sao";
			_betchawnsxChanLe["txtLe"].restrict = "0-9";
			_betchawnsxChanLe["txtLe"].maxChars = 9;
			
			_betArr.push(_betchawnsxChanLe["txtChan"], _betchawnsxChanLe["txtLe"]);
			_totalBetMoney["txtSum"].text = "Tổng: 0 sao";
			//_betchawnsxChanLe["txtSum"].text = "";
			
			_bettingFish["btnHoanThanh"].buttonMode = true;
			_bettingFish["btnHuy"].buttonMode = true;
			
			_bettingFish["btnHoanThanh"].addEventListener(MouseEvent.CLICK, onClickSendData);
			_bettingFish["btnHuy"].addEventListener(MouseEvent.CLICK, onCancelData);
			
			MyData.getInstance().addEventListener("changeMoney", onChangeMoney);
			
			GameData.getInstance().addEventListener("canGetResult", getResult);
			GameData.getInstance().addEventListener("checkGetResultAgain", getResultAgain);
			RequestController.getInstance().addEventListener("startAgain", getBettingAgain);
			
			for (i = 0; i < 10; i++) 
			{
				_betArr[i].addEventListener(Event.CHANGE, onChangeTextBettingFish);
			}
			for (i = 10; i < 12; i++) 
			{
				_betArr[i].addEventListener(Event.CHANGE, onChangeTextBettingChanle);
			}
			
			for (i = 0; i < 12; i++) 
			{
				_betArr[i].addEventListener(FocusEvent.FOCUS_IN, onFocusInTextInput);
				_betArr[i].addEventListener(FocusEvent.FOCUS_OUT, onFocusOuTextInput);
			}
			
			var str:String = "";
			for (i = 0; i < 7; i++) 
			{
				str += "0";
				_totalSum.txt_num.text = str;
			}
			
			_objectOne = new Object();
			_objectTotal = new Object();
			for (i = 1; i < 13; i++) 
			{
				_objectTotal["fish" + String(i)] = ["x 1", 0];
				//_objectOne["fish" + i] = ["x 1", 0];
			}
			
			_historyBet.btn_lichsubat.buttonMode = true;
			GameData.getInstance().addEventListener("quithantai", getTotalMoneyToday );
			_historyBet.btn_lichsubat.addEventListener(MouseEvent.CLICK, clickShowHistoryBet);
			_historyBet.btn_huongdan.addEventListener(MouseEvent.CLICK, clickShowTutBoard);
			
			addDataBetting();
			
			addTooltip();
			
			_showTime.addEventListener(MouseEvent.MOUSE_OVER, overTime);
			_showTime.addEventListener(MouseEvent.MOUSE_OUT, outTime);
		}
		
		private function clickShowTutBoard(e:MouseEvent):void 
		{
			if (!_popupTutorial) 
			{
				_popupTutorial = new TutorialBoard();
				addChild(_popupTutorial);
			}
			else 
			{
				_popupTutorial.visible = true;
			}
			
			setChildIndex(_popupTutorial, numChildren - 1);
			
			_popupTutorial.addEventListener("close", onCloseTut);
		}
		
		private function onCloseTut(e:Event):void 
		{
			_popupTutorial.visible = false;
			_popupTutorial.removeEventListener("close", onCloseTut);
		}
		
		private function outTime(e:MouseEvent):void 
		{
			if (_tipQTT)
			{
				_tipQTT.visible = false;
			}
		}
		
		private function overTime(e:MouseEvent):void 
		{
			if (_tipQTT)
			{
				_tipQTT.visible = true;
				_tipQTT.x = 660;
				_tipQTT.y = 110;
				_tipQTT.showTextTime("Thời gian còn lại")
			}
		}
		
		private function addTooltip():void 
		{
			
			_tipAll = new Tooltip();
			addChild(_tipAll);
			_tipAll.x = 300;
			_tipAll.y = 300;
			_tipAll.visible = false;
			//txttip
			
			_tipQTT = new TooltipTimeAndQTT();
			addChild(_tipQTT);
			_tipQTT.x = 180;
			_tipQTT.y = 180;
			_tipQTT.visible = false;
			addShowQTT();
			
			
			
		}
		
		private function addShowQTT():void 
		{
			_timeShowQTT.addEventListener(TimerEvent.TIMER, onCompleteShowQTT);
			_timeShowQTT.start();
		}
		
		private function onCompleteShowQTT(e:TimerEvent):void 
		{
			_timeShowQTT.stop();
			_timeShowQTT.removeEventListener(TimerEvent.TIMER, onCompleteShowQTT);
			var j:int;
			if (_tipQTT)
			{
				_tipQTT.x = 260;
				_tipQTT.y = 190;
				_tipQTT.visible = true;
				_tipQTT.showTextQTT("Quỹ thần tài đang đầy ăn bớt đi nào");
				if (_showTime)
				{
					_showTime.removeEventListener(MouseEvent.MOUSE_OVER, overTime);
					_showTime.removeEventListener(MouseEvent.MOUSE_OUT, outTime);
				}
			}
			
			_timeVisibleQTT.addEventListener(TimerEvent.TIMER, onCompleteVisibleQTT);
			_timeVisibleQTT.start();
		}
		
		private function onCompleteVisibleQTT(e:TimerEvent):void 
		{
			_timeVisibleQTT.stop();
			_timeVisibleQTT.removeEventListener(TimerEvent.TIMER, onCompleteVisibleQTT);
			var j:int;
			if (_tipQTT)
			{
				_tipQTT.visible = false;
				if (_showTime)
				{
					_showTime.addEventListener(MouseEvent.MOUSE_OVER, overTime);
					_showTime.addEventListener(MouseEvent.MOUSE_OUT, outTime);
				}
			}
			addShowQTT();
		}
		
		private function onClickSoundOnOff(e:MouseEvent):void 
		{
			if (GameData.getInstance().checkSoundOn) 
			{
				GameData.getInstance().checkSoundOn = false;
				_settingSound.gotoAndStop(2);
				
			}
			else 
			{
				GameData.getInstance().checkSoundOn = true;
				_settingSound.gotoAndStop(1);
			}
		}
		
		private function clickShowHistoryBet(e:MouseEvent):void 
		{
			//_historyBet.removeEventListener(MouseEvent.CLICK, clickShowHistoryBet);
			if (GameData.getInstance().checkSoundOn) 
			{
				SoundManager.getInstance().playSound("Click1", 1);
			}
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			trace(obj[Const.PLAYER_NAME], obj[Const.SEND_GAME_ID]);
			MyData.getInstance().page = 0;
			RequestController.getInstance().sendDataHistory(obj);
			
			dispatchEvent(new Event("addHistory"));
		}
		
		private function onFocusInTextInput(e:FocusEvent):void 
		{
			var textfield:TextField = e.currentTarget as TextField;
			if (textfield.text == "Nhập sao") 
			{
				textfield.text = ""
			}
		}
		
		private function onFocusOuTextInput(e:FocusEvent):void 
		{
			var textfield:TextField = e.currentTarget as TextField;
			if (textfield.text == "Nhập sao" || textfield.text == "") 
			{
				textfield.text = "Nhập sao"
			}
		}
		
		private function getBettingAgain(e:Event):void 
		{
			trace("add event start again")
			checkTimeRemain();
		}
		
		private function getTotalMoneyToday(e:Event):void 
		{
			var str:String = "";
			var totalMoney:int = GameData.getInstance().totalMoneyToday;
			var arr:Array = [];
			
			
			for (i = 0; i < 7 - String(totalMoney).length; i++) 
			{
				str += "0";
			}
			trace("1: ", str)
			str += String(totalMoney);
			trace("2: ", str)
			_totalSum.txt_num.text = str;
			_totalSum.txt_num.selectable = false;
		}
		
		private function onChangeTextBettingChanle(e:Event):void 
		{
			var textfield:TextField = e.currentTarget as TextField;
			
			var sum:int = 0;
			for (i = 0; i < 10; i++) 
			{
				sum += int(_betArr[i].text) * 1;
			}
			sum = sum + int(_betArr[10].text) + int(_betArr[11].text);
			
			if (sum > MyData.getInstance().myMoney) 
			{
				dispatchEvent(new Event("not_enough_money"))
				textfield.text = "Nhập sao";
			}
			else 
			{
				_totalBetMoney["txtSum"].text = "Tổng: " + format(sum) + " sao";
			}
		}
		
		private function onChangeTextBettingFish(e:Event):void 
		{
			var textfield:TextField = e.currentTarget as TextField;
			
			var sum:int = 0;
			for (i = 0; i < 10; i++) 
			{
				sum += int(_betArr[i].text) * 1;
			}
			sum = sum + int(_betArr[10].text) + int(_betArr[11].text);
			
			if (sum > MyData.getInstance().myMoney) 
			{
				dispatchEvent(new Event("not_enough_money"))
				textfield.text = "Nhập sao";
			}
			else 
			{
				_totalBetMoney["txtSum"].text = "Tổng: " + format(sum) + " sao";
			}
			
		}
		
		private function getResultAgain(e:Event):void 
		{
			trace("chưa đc lấy kết quả")
			_timeOutGetResult++;
			if (_timeOutGetResult < 40) 
			{
				checkCanGetResult();
			}
			
		}
		
		
		
		private function onChangeMoney(e:Event):void 
		{
			/*if (_showMoney)
			{
				_showMoney.txtCoin.text = format(MyData.getInstance().myMoney) + " Vcoin";
			}*/
			
			
			
		}
		
		private function onClickSendData(e:MouseEvent):void 
		{
			if (GameData.getInstance().checkSoundOn) 
			{
				SoundManager.getInstance().playSound("Click1", 1);
			}
			
			//_myChannelClick = _mySoundClick.play();
			//dispatchEvent(new Event("showNotice"));
			agreeSendData();
			
			
		}
		
		private function addDataBetting():void 
		{
			
			
			if (!_popupResultBetting)
			{
				_popupResultBetting = new PopupResultBetting();
				addChild(_popupResultBetting);
				_popupResultBetting.x = 530;
				_popupResultBetting.y = 580;
				
			}
		}
		
		public function agreeSendData():void 
		{
			var arr:Array = [];
			for (var i:int = 0; i < _betArr.length; i++) 
			{
				arr.push(_betArr[i].text);
				if (i < 10) 
				{
					_objectOne["fish" + String(i + 1)] = [_multiArr[i].text, _betArr[i].text];
					trace("add cac thu vao obj oner: ", i, _multiArr[i].text, _betArr[i].text)
				}
				else 
				{
					_objectOne["fish" + String(i + 1)] = ["x 1.9", _betArr[i].text];
					trace("add cac thu vao obj oner: ", i, _betArr[i].text)
				}
			}
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			obj[Const.BET] = arr;
			RequestController.getInstance().sendDataBetting(obj);
			RequestController.getInstance().addEventListener("send_error", onSendDataAgain);
			RequestController.getInstance().addEventListener("send_complete", onSendDataSuccess);
		}
		
		private function onSendDataSuccess(e:Event):void 
		{
			RequestController.getInstance().removeEventListener("send_error", onSendDataAgain);
			RequestController.getInstance().removeEventListener("send_complete", onSendDataSuccess);
			
			for (var i:int = 0; i < _betArr.length; i++) 
			{
				_objectTotal["fish" + String(i + 1)][0] = _objectOne["fish" + String(i + 1)][0];
				trace("khi send data success", i)
				//_betArr[i].text = String(0);
				trace("x 10: ", _objectOne["fish" + String(i + 1)][0])
				trace("x 10: ", _objectOne["fish" + String(i + 1)][1])
				trace("---***---")
				trace("obj total: ", _objectTotal["fish" + String(i + 1)][0])
				trace("objtoal:  ", _objectTotal["fish" + String(i + 1)][1])
				if (_objectOne["fish" + String(i + 1)][1] == "Nhập sao") 
				{
					_objectTotal["fish" + String(i + 1)][1] = _objectTotal["fish" + String(i + 1)][1] + 0;
				}
				else 
				{
					_objectTotal["fish" + String(i + 1)][1] = _objectTotal["fish" + String(i + 1)][1] + 
														int(_objectOne["fish" + String(i + 1)][1]);
				}
				trace("sau khi cong: ", _objectTotal["fish" + String(i + 1)][0], _objectTotal["fish" + String(i + 1)][1])
				trace("==================================")
			}
			
			GameData.getInstance().totalEachOfFish = _objectTotal;
			
			//_betchawnsxChanLe["txtChan"].text = "0";
			//_betchawnsxChanLe["txtLe"].text = "0";
			
			//_showMoney.txtCoin.text = format(MyData.getInstance().myMoney) + " Vcoin";
			
			dispatchEvent(new Event("sendDataSuccess"));
		}
		
		
		private function onSendDataAgain(e:Event):void 
		{
			RequestController.getInstance().removeEventListener("send_error", onSendDataAgain);
			RequestController.getInstance().removeEventListener("send_complete", onSendDataSuccess);
			agreeSendData();
			trace("send data error")
		}
		
		private function onCancelData(e:MouseEvent):void 
		{
			if (GameData.getInstance().checkSoundOn) 
			{
				SoundManager.getInstance().playSound("Click1", 1);
			}
			
			//_myChannelClick = _mySoundClick.play();
			for (var i:int = 0; i < _betArr.length; i++) 
			{
				_betArr[i].text = "Nhập sao";
			}
		}
		
		public function timeRemain(time:int):void 
		{
			if (_timer) 
			{
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, onTimer);
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			}
			
			_count = 0;
			
			_timeRemain = time - 50;
			trace("time remain tra ve: ", _timeRemain)

			if (_timeRemain <= 0) 
			{
				_timeRemain = 0;
				
			}
			else 
			{
				_timer = new Timer(1000, time);
				_timer.addEventListener(TimerEvent.TIMER, onTimer);
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
				_timer.start();
			}
			
			trace("thang nay dang choi: ")
			MyData.getInstance().canStartGame = false;
			
			testGetResult();
		}
		
		private function onTimerComplete(e:TimerEvent):void 
		{
			
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER, onTimer);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			
			checkCanGetResult();
			
		}
		
		private function checkCanGetResult():void 
		{
			timerGet = new Timer(500, 1);
			timerGet.addEventListener(TimerEvent.TIMER_COMPLETE, onCompleteCheck);
			timerGet.start();
			
		}
		
		private function onCompleteCheck(e:Event):void 
		{
			RequestController.getInstance().getInfo(true);
		}
		
		private function onTimer(e:TimerEvent):void 
		{
			_count++;
			_timeRemain--;
			//_timeGetResult--;
			if (_timeRemain <= 10) 
			{
				GameData.getInstance()._checkHaveResult = false;
			}
			if (_timeRemain <= 0) 
			{
				_timeRemain = 0;
				if (_timer) 
				{
					_timer.stop();
					_timer.removeEventListener(TimerEvent.TIMER, onTimer);
					_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
				}
				checkCanGetResult();
			}
			
			var sec:Number = (_timeRemain - Math.floor(_timeRemain / 60) * 60);
			var loc2:String;
			if(sec < 10)
			{
				loc2 = "0" + Math.floor(_timeRemain / 60) + ":" + "0" + sec;
			}
			else
			{
				loc2 = "0" + Math.floor(_timeRemain / 60) + ":" + sec;
			}
			
			if (_showTime)
			{
				_showTime.txtTime.text = "Thời gian: " + loc2;
			}
			
			
			if (_count == 10) 
			{
				checkTimeRemain();
			}
		}
		
		private function checkTimeRemain():void 
		{
			RequestController.getInstance().getInfo();
		}
		
		private function getResult(e:Event):void 
		{
			
			//GameData.getInstance().callResult = true;
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			
			
			RequestController.getInstance().getResult(obj);
		}
		
		public function getMulti(str:String):void 
		{
			if (str)
			{
				var arr:Array = str.split(",");
				_arrAnimalHS = arr;
				for (var i:int = 0; i < arr.length; i++) 
				{
					
					_multiArr[i].text = "x " + String(arr[i]);
					
					
				}
				GameData.getInstance().checkLoading = false;
				
				//lay thong tin cho vao tooltip
				
				for (var j:int = 0; j < _arrImageAnimal.length; j++) 
				{
					//_countName = j;
					//_arrImageAnimal[j].name = String(i + 1);
					_arrImageAnimal[j].addEventListener(MouseEvent.MOUSE_OVER, overImage)
					_arrImageAnimal[j].addEventListener(MouseEvent.MOUSE_OUT, outImage)
				}
			}
			
			
		}
		
		private function outImage(e:MouseEvent):void 
		{
			if (_tipAll)
			{
				_tipAll.visible = false;
			}
		}
		
		private function overImage(e:MouseEvent):void 
		{
			trace(e.currentTarget.name)
			var _name:String = String(e.currentTarget.name)
			var _mc:MovieClip = e.currentTarget as MovieClip;
			switch (_name) 
			{
				case "conca":
					_countName = 0;
					GameData.getInstance().arrTip = ["Cá", "ca", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 220;
					}
				break;
				case "concua":
					_countName = 1;
					GameData.getInstance().arrTip = ["Cua", "cua", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 255;
					}
				break;
				case "consua":
					_countName = 2;
					GameData.getInstance().arrTip = ["Sứa", "sua", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 290;
					}
				break;
				case "conoc":
					_countName = 3;
					GameData.getInstance().arrTip = ["Ốc", "oc", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 325;
					}
				break;
				case "contom":
					_countName = 4;
					GameData.getInstance().arrTip = ["Tôm", "tom", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 360;
					}
				break;
				case "conruabien":
					_countName = 5;
					GameData.getInstance().arrTip = ["Rùa", "rua", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 395;
					}
				break;
				case "concangua":
					_countName = 6;
					GameData.getInstance().arrTip = ["Cá Ngựa", "cangua", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 430;
					}
				break;
				case "conranbien":
					_countName = 7;
					GameData.getInstance().arrTip = ["Rắn Biển", "ranbien", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 465;
					}
				break;
				case "consam":
					_countName = 8;
					GameData.getInstance().arrTip = ["Sam", "caduoi", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 510;
					}
				break;
				case "concaugai":
					_countName = 9;
					GameData.getInstance().arrTip = ["Cầu Gai", "caugai", _arrAnimalHS[_countName]];
					if (_tipAll)
					{
						_tipAll.visible = true;
						_tipAll.x = 750;
						_tipAll.y = 545;
					}
				break;
				default:
			}
			
		}
		
		private function testGetResult():void 
		{
			timerTest = new Timer(1000);
			timerTest.addEventListener(TimerEvent.TIMER, onTimerTest);
			timerTest.start();
		}
		
		private function onTimerTest(e:TimerEvent):void 
		{
			var obj:Object = new Object();
			obj[Const.PLAYER_NAME] = MyData.getInstance().myName;
			obj[Const.SEND_GAME_ID] = GameData.getInstance().idGame;
			
			
			RequestController.getInstance().getResult(obj);
		}
		public function removePopUp():void
		{
			if (_popupResultBetting)
			{
				_popupResultBetting.removeAllEvent();
				removeChild(_popupResultBetting);
				_popupResultBetting = null;
			}
		}
		public function removeAllEvent():void
		{
			
			MyData.getInstance().removeEventListener("changeMoney", onChangeMoney);
			
			GameData.getInstance().removeEventListener("canGetResult", getResult);
			GameData.getInstance().removeEventListener("checkGetResultAgain", getResultAgain);
			RequestController.getInstance().removeEventListener("startAgain", getBettingAgain);
			//if (_settingSound)
			//{
				//_settingSound.removeEventListener(MouseEvent.CLICK, onClickSoundOnOff);
			//}
			
			if (_betArr && _betArr.length > 0)
			{
				for (i = 0; i < 10; i++) 
				{
					_betArr[i].removeEventListener(Event.CHANGE, onChangeTextBettingFish);
				}
				for (i = 10; i < 12; i++) 
				{
					_betArr[i].removeEventListener(Event.CHANGE, onChangeTextBettingChanle);
				}
			}
			
			
			GameData.getInstance().removeEventListener("quithantai", getTotalMoneyToday );
			
			if (_popupTutorial) 
			{
				_popupTutorial.removeEventListener("close", onCloseTut);
				_popupTutorial.removeAllEvent();
				
				removeChild(_popupTutorial);
				_popupTutorial = null;
			}
			
			
			//if (_popupResultBetting)
			//{
				//removeChild(_popupResultBetting);
				//_popupResultBetting = null; 
			//}
			if (_totalBetMoney)
			{
				removeChild(_totalBetMoney);
				_totalBetMoney = null;
			}
			if (_tipAll)
			{
				removeChild(_tipAll);
				_tipAll = null;
			}
			if (_tipQTT)
			{
				removeChild(_tipQTT);
				_tipQTT = null;
			}
			if (_showTime)
			{
				removeChild(_showTime);
				_showTime = null; 
			}
			if (_bettingFish)
			{
				_bettingFish["btnHoanThanh"].removeEventListener(MouseEvent.CLICK, onClickSendData);
				_bettingFish["btnHuy"].removeEventListener(MouseEvent.CLICK, onCancelData);
				removeChild(_bettingFish);
				_bettingFish = null; 
				
			}
			if (_betchawnsxChanLe)
			{
				removeChild(_betchawnsxChanLe);
				_betchawnsxChanLe = null; 
			}
			
			if (_historyBet)
			{
				_historyBet.btn_lichsubat.removeEventListener(MouseEvent.CLICK, clickShowHistoryBet);
				_historyBet.btn_huongdan.removeEventListener(MouseEvent.CLICK, clickShowTutBoard);
				_settingSound.removeEventListener(MouseEvent.CLICK, onClickSoundOnOff);
				removeChild(_historyBet);
				_historyBet = null; 
			}
			
			/*if (_historyChangeMoney)
			{
				removeChild(_historyChangeMoney);
				_historyChangeMoney = null; 
			}*/
			if (_totalSum) 
			{
				removeChild(_totalSum);
				_totalSum = null;
			}
			
			_betArr = [];
			_multiArr = [];
			
			if (_timer) 
			{
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, onTimer);
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			}
			
			if (timerGet) 
			{
				timerGet.stop();
				timerGet.removeEventListener(TimerEvent.TIMER_COMPLETE, onCompleteCheck);
			}
			
			if (timerTest) 
			{
				timerTest.stop();
				timerTest.removeEventListener(TimerEvent.TIMER, onTimerTest);
			}
			if (_timeShowQTT)
			{
				_timeShowQTT.stop();
				_timeShowQTT.removeEventListener(TimerEvent.TIMER, onCompleteShowQTT);
			}
			if (_timeVisibleQTT)
			{
				_timeVisibleQTT.stop();
				_timeVisibleQTT.removeEventListener(TimerEvent.TIMER, onCompleteVisibleQTT);
			}
			//_timeOutGetResult = 0;
			if (_showTime)
			{
				_showTime.removeEventListener(MouseEvent.MOUSE_OVER, overTime);
				_showTime.removeEventListener(MouseEvent.MOUSE_OUT, outTime);
			}
			
			var j:int;
			if (_arrImageAnimal && _arrImageAnimal.length > 0)
			{
				for (j = 0; j < _arrImageAnimal.length; j++) 
				{
					_arrImageAnimal[j].removeEventListener(MouseEvent.MOUSE_OVER, overImage)
					_arrImageAnimal[j].removeEventListener(MouseEvent.MOUSE_OUT, outImage)
				}
			}
			
			
			
			
		}
		
		public function removeAllTimer():void 
		{
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER, onTimer);
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
		}
	}

}